package com.carsa.blackberry.domain.dto;

import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.util.DateUtil;
import com.carsa.blackberry.util.XMLHandler;
import com.carsa.blackberry.util.XMLPortable;
import com.carsa.blackberry.util.persistence.Persistable;

/**
 * @author Nahuel Barrios.
 * @since 23/08/2010.
 * @version 1.0
 */
public class ReasonsQueueDTO implements Persistable, XMLPortable {

    private static final String XML_ELEMENT_ROOT        = "data";
    private static final String XML_ELEMENT_LAST_UPDATE = "lastUpdate";
    private static final String XML_ELEMENT_REASONS     = "reasons";

    // private String _lastUpdate;
    private Date                _lastUpdate;
    private Hashtable           _reasons                = new Hashtable();

    public ReasonsQueueDTO() {
	super();
    }

    public ReasonsQueueDTO(String xmlSource) {
	super();
	this.initializeFromXML(xmlSource);
    }

    public String getStringToPersist() {
	return this.toXMLString();
    }
    
    public ReasonDTO get(String grund){
	return (ReasonDTO)this._reasons.get(grund);
    }

    public Vector getSubreasonsWithFlepaX() {
	Vector ret = new Vector();

	Enumeration reasons = _reasons.elements();
	while (reasons.hasMoreElements()) {
	    ReasonDTO aReason = (ReasonDTO) reasons.nextElement();
	    Enumeration elements = aReason.getSubreasonsWithFlepa(MainKeys.REASON_FLEPA_X).elements();
	    while (elements.hasMoreElements()) {
		SecondReasonDTO aSubreason = (SecondReasonDTO) elements.nextElement();
		ret.addElement(aSubreason);
	    }
	}

	return ret;
    }

    /**
     * @param xmlSource
     *            El {@link String} en formato XML del cual se deben recuperar los atributos para setear el objeto this.
     */
    public void initializeFromXML(String xmlSource) {
	XMLHandler xml = new XMLHandler(xmlSource);

	_lastUpdate = DateUtil.getFromString(xml.getElementValue(XML_ELEMENT_LAST_UPDATE), DateUtil.DATE_SEPARATOR_BAR);

	xml = new XMLHandler(xml.getElementValue(XML_ELEMENT_REASONS));
	int elementsCount = xml.getElementCount();

	for (int idx = 0; idx < elementsCount; idx++) {
	    ReasonDTO aReason = new ReasonDTO(xml.getFirstElementValueOverIteration());
	    _reasons.put(aReason.getReasonCode(), aReason);
	}
    }

    /**
     * @return the lastUpdate
     */
    public Date getLastUpdate() {
	return _lastUpdate;
    }

    /**
     * @param lastUpdate
     *            the lastUpdate to set
     */
    public void setLastUpdate(Date lastUpdate) {
	this._lastUpdate = lastUpdate;
    }

    /**
     * @return the reasons
     */
    public Hashtable getReasons() {
	return _reasons;
    }

    /**
     * @param reasons
     *            the reasons to set
     */
    public void setReasons(Hashtable reasons) {
	this._reasons = reasons;
    }

    public String toString() {
	return "ReasonsQueueDTO [lastUpdate=" + _lastUpdate + ", reasons=" + _reasons + "]";
    }

    public String getPersistPath() {
	return MainKeys.FILE_PATH_REASONS;
    }

    public String toXMLString() {
	XMLHandler xmlReasons = new XMLHandler(XML_ELEMENT_ROOT, null, null);

	xmlReasons.addElementAtRoot(XML_ELEMENT_LAST_UPDATE, null, DateUtil.exportDateAs(Calendar.getInstance()
	        .getTime(), DateUtil.DATE_ORDER_DD_MM_AAAA, DateUtil.DATE_SEPARATOR_BAR));

	XMLHandler reasonsElement = new XMLHandler(XML_ELEMENT_REASONS, null, null);
	Enumeration reasons = _reasons.elements();
	while (reasons.hasMoreElements()) {
	    ReasonDTO aReason = (ReasonDTO) reasons.nextElement();

	    reasonsElement.addElementAtRoot(aReason.toXMLString());
	}
	xmlReasons.addElementAtRoot(reasonsElement.toXMLString());

	return MainKeys.XML_VERSION_TAG + xmlReasons.toString();
    }

}
